import * as Service from '../services/weChat';
import { message } from 'antd';
import { getDay, defaultDate } from '../utils/date';

const name = ['萌岛乐生活'];


export default {
  namespace: 'weChat',
  state: {
    UserSummarySum: {},
    UserSummary: {},
    UserCumulate: {},
    UserShareSum: {},
    UpStreamMsgSum: {},
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },

  },
  effects: {
    *all({ payload }, { call, put }) {
      const { defaultDataSum, defaultData } = payload;
      yield put({ type: 'queryUserSummarySum', payload: defaultDataSum });
      yield put({ type: 'queryUpStreamMsgSum', payload: defaultDataSum });
      yield put({ type: 'queryUserCumulate', payload: defaultData });
      yield put({ type: 'queryUserShareSum', payload: defaultDataSum });
    },
    *queryUserSummarySum({ payload }, { call, put }) {
      const { data } = yield call(Service.queryUserSummarySum, payload);
      if (data.success && data) {
        yield put({ type: 'save', payload: { UserSummarySum: data.data[0] } });
      } else {
        message.error('累计每日粉丝关注获取失败');
      }
      console.log('queryUserSummarySum');
      console.log(data);
    },
    *queryUserSummary({ payload }, { call, put }) {
      const { data } = yield call(Service.queryUserSummary, payload);
      if (data.success && data) {
        yield put({ type: 'save', payload: { UserSummary: data.data[0] } });
      } else {
        message.error('累计每日粉丝关注来源获取失败');
      }
      console.log('queryUserSummary');
      console.log(data);
    },
    *queryUserCumulate({ payload }, { call, put }) {
      const { data } = yield call(Service.queryUserCumulate, payload);
      if (data.success && data) {
        yield put({ type: 'save', payload: { UserCumulate: data.data[0] } });
      } else {
        message.error('累计粉丝关注获取失败');
      }
      console.log('queryUserCumulate');
      console.log(data);
    },
    *queryUserShareSum({ payload }, { call, put }) {
      const { data } = yield call(Service.queryUserShareSum, payload);
      if (data.success && data) {
        yield put({ type: 'save', payload: { UserShareSum: data.data[0] } });
      } else {
        message.error('累计粉丝分享获取失败');
      }
      console.log('queryUserShareSum');
      console.log(data);
    },
    *queryUpStreamMsgSum({ payload }, { call, put }) {
      const { data } = yield call(Service.queryUpStreamMsgSum, payload);
      if (data.success && data) {
        yield put({ type: 'save', payload: { UpStreamMsgSum: data.data[0] } });
      } else {
        message.error('累计粉丝分享获取失败');
      }
      console.log('queryUpStreamMsgSum');
      console.log(data);
    },

  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/WeChat') {
          const defaultDataSum = {
            name,
            date: defaultDate(getDay(3), getDay(32)),
          };
          const defaultData = {
            name,
            date: [getDay(3)],
          };
          dispatch({ type: 'all', payload: { defaultDataSum, defaultData } });
        }
      });
    },
  },
};
